package com.yxinsur.imc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxinsur.imc.business.domain.ImcOrderImg;
import com.yxinsur.imc.business.enums.IsDeletedEnum;
import com.yxinsur.imc.business.form.OrderImgForm;
import com.yxinsur.imc.business.mapper.ImcOrderImgMapper;
import com.yxinsur.imc.business.mapper.ImcOrderMapper;
import com.yxinsur.imc.business.service.IImcOrderImgService;
import com.yxinsur.imc.business.vo.OrderImgVo;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import com.yxinsur.imc.common.validator.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 保单影像件Service业务层处理
 * 
 * @author imc
 * @date 2021-03-29
 */
@Service
public class ImcOrderImgServiceImpl extends ServiceImpl<ImcOrderImgMapper, ImcOrderImg> implements IImcOrderImgService
{
    private static final Logger logger = LoggerFactory.getLogger(ImcOrderImgServiceImpl.class);

    @Autowired
    private ImcOrderMapper imcOrderMapper;
    @Autowired
    private ImcOrderImgMapper imcOrderImgMapper;

    /**
     * 查询保单影像件列表
     *
     * @param orderId 保单ID
     * @return 保单集合
     */
    @Override
    public List<OrderImgVo> selectOrderImgListByOrderId(String orderId) {
        logger.info("保单影像件列表查询：{}",orderId);
        QueryWrapper<ImcOrderImg> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        wrapper.eq("is_deleted",IsDeletedEnum.N.getCode());
        wrapper.eq("tenant_id",SecurityUtils.getTenantId());
        List<ImcOrderImg> list = this.imcOrderImgMapper.selectList(wrapper);
        List<OrderImgVo> imgs = new ArrayList<>();
        for (ImcOrderImg orderImg:list){
            OrderImgVo vo = new OrderImgVo();
            BeanUtils.copyProperties(orderImg,vo);
            imgs.add(vo);
        }
        return imgs;
    }

    /**
     * 新增保单影像件
     *
     * @param imgForm 保单影像件数据
     * @return 保单集合
     */
    @Override
    public int insertImcOrderImg(OrderImgForm imgForm) {
        logger.info("保单影像件新增：{}",imgForm.toString());
        Assert.isNull(imgForm.getOrderId(),"未关联任何保单信息，保单id为必传字段");
        ImcOrderImg img = new ImcOrderImg();
        BeanUtils.copyProperties(imgForm,img);
        img.setCreateTime(DateUtils.getNowDate());
        img.setCreateId(SecurityUtils.getUsername());
        img.setIsDeleted(IsDeletedEnum.N.getCode());
        img.setTenantId(SecurityUtils.getTenantId());
        return this.imcOrderImgMapper.insert(img);
    }
}
